<template>
  <div>
    <div v-show="!showMap" class="dashboard-editor-container">
      <github-corner @switchMethod="switchMethod" class="github-corner"/>

      <panel-group :count="newDataCount" @handleSetLineChartData="handleSetLineChartData"/>

      <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
        <line-chart :data="downInfo" :chart-data="lineChartData"/>
      </el-row>
    </div>
    <div v-show="showMap" class="dashboard-editor-container">
      <github-corner @switchMethod="switchMethod" class="github-corner"/>
      <my-map :markers="markers"></my-map>
    </div>
  </div>
</template>

<script>
import PanelGroup from './components/PanelGroup'
import LineChart from './components/LineChart'
import MyMap from './components/MyMap'
import GithubCorner from '@/components/GithubCorner'

const lineChartData = {
  newOrder: {
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  newUser: {
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  newDriver: {
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  newCar: {
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
}
import { getAllCar, getAllData } from '@/api/home'

export default {
  name: 'HomeAdmin',
  components: {
    PanelGroup,
    LineChart,
    MyMap,
    GithubCorner
  },
  data() {
    return {
      showMap: false,
      chartData: null,
      lineChartData: lineChartData.newOrder,
      allLineChartData: {
        newOrder: {
          actualData: [120, 82, 91, 154, 162, 140, 145]
        },
        newUser: {
          actualData: [180, 160, 151, 106, 145, 150, 130]
        },
        newDriver: {
          actualData: [120, 90, 100, 138, 142, 130, 130]
        },
        newCar: {
          actualData: [120, 82, 91, 154, 162, 140, 130]
        }
      },
      downInfo: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      newDataCount: {
        newOrder: 3000,
        newUser: 2000,
        newDriver: 2000,
        newCar: 1000
      },
      center: [108.909342, 34.218990],
      plugins: ['MapType', 'Overview', 'ToolBar', 'Scale'],
      markers: [
        {
          carId: 1,
          position: [108.909342, 34.218990]
        }, {
          carId: 2,
          position: [108.919342, 34.218990]
        }, {
          carId: 3,
          position: [108.929342, 34.218990]
        }
      ]
    }
  },
  created() {
    this.getData()
    this.getCars()
    setInterval(() => {
      if (this.showMap) {
        this.getCars()
      } else {
        this.getData()
      }
    }, 5000)
  },
  methods: {
    async getData() {
      const { data } = await getAllData()
      console.log(data)
      const { orders, driver, cars, users } = data
      this.allLineChartData.newCar.actualData = cars
      this.allLineChartData.newDriver.actualData = driver
      this.allLineChartData.newOrder.actualData = orders
      this.allLineChartData.newUser.actualData = users
      this.chartData = data
      this.lineChartData = this.allLineChartData.newOrder
      this.newDataCount = data
      this.downInfo = data.date
    },
    async getCars() {
      const { data } = await getAllCar()
      console.log(data)
      this.markers = data
    },
    handleSetLineChartData(type) {
      this.lineChartData = this.allLineChartData[type]
    },
    switchMethod() {
      this.showMap = !this.showMap
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
